//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
export default {
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      coursesId: "",
      chatStrId: "",
      errorShow: false,
      succeesShow: false,
      efficacyType: 1,
      goodsUrl: "",
      courses: {},
      hasCopyButton: false,
      videoFlag: false,
      coursesLink: "https://dev8.yunzmall.com/addons/yun_shop/?menu#/category?i=1&type=5&shop_id&mid=166015",
      durationTime: "", //显示的时间
      duration: "", //总秒数
      description: "", //详情
      presentTime:'',
      textDescription:'',
      timeFlag:false
    };
  },
  //监听属性 类似于data概念
  computed: {
    beforeDay() {
      return function(time) {
        let endTime = time * 1000;
        if (!Number(time)) return;
        if(time === 0){
          this.timeFlag = false;
          this.videoFlag = true;
        }
        return endTime - Number(this.presentTime);
      }
    },
    backDay() {
      if(this.courses.back_time_start && this.courses.back_time_end) {
        if(this.presentTime > (this.courses.back_time_start * 1000) && this.presentTime < (this.courses.back_time_end * 1000)) {
          return true;
        }  else {
          return false;
        }
      }
    },
    liveDay() {
      if(this.courses.live_time_start && this.courses.live_time_end) {
        if(this.presentTime > (this.courses.live_time_start * 1000) && this.presentTime < (this.courses.live_time_end * 1000)) {
          return true;
        }  else {
          return false;
        }
      }
    }
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    onPlayerPlay(event) {
      this.videoFlag = false;
    },
    onLoadedmetadata(event) {
      let videoTime = document.getElementById("goods_video");
      let duration = parseInt(videoTime.duration);
      this.duration = duration;
    },
    onPlayerEnded(event) {
      this.videoFlag = true;
    },
    onPlayer(event) {
      event.preventDefault();
    },
    videoPay() {
      this.videoFlag = false;
      this.$refs.goodsVideo.play();
    },
    sucClick() {
      this.succeesShow = false;
    },
    resetClick() {
      this.errorShow = false;
    },
    datumTo() {
      this.$router.push(this.fun.getUrl("courseDatum", { id: this.courses.id }, {}));
    },
    copyUrl() {
      this.$toast("复制成功");
    },
    computingTime(afterTime, backTime) {
      if(backTime === 0) {
        this.videoFlag = true;
      }
      if (!Number(backTime)) return;
      let res = Number(afterTime) - Number(backTime);
      if (res >= 0) {
        this.timeFlag = true;
      } else {
        this.timeFlag = false;
        this.videoFlag = true;
      }
    },
    coursesDetail() {
      let query = {
        courses_id: Number(this.coursesId),
        chat_str_id: this.chatStrId
      };
      if (this.fun.getKey("client") === "work") {
        query = { ...this.getConfig, ...query };
      }
      $http
        .post("plugin.wechat-video-courses.frontend.controller.manage.courses.detail", query, "loading")
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            this.courses = data.courses;
            this.hasCopyButton = data.has_copy_button;
            this.coursesLink = data.courses_link;
            this.description = this.courses?.introduce?.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
            this.textDescription = this.courses?.lecturer_introduce?.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
            this.computingTime(this.courses?.limit_time *1000,this.presentTime);
          } else {
            this.$toast(res.msg);
          }
        })
        .catch(err => {
          console.log("getUserData::err", err);
        });
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    
  },

  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
      this.chatStrId = window.localStorage.getItem("chat_id") || "";
    }
    this.presentTime = new Date().getTime();
    if(this.$route.params.id) {
      this.coursesId = this.$route.params.id;
      this.coursesDetail();
    }
  } //如果页面有keep-alive缓存功能，这个函数会触发
};
